#-*- coding: utf-8 -*-
import httplib2, json, urllib
from menu_dict import menu
conn = httplib2.Http('.cache')

def get_access_token(app_id, app_secret):
    request_url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s' \
                    % (app_id, app_secret)
    resp, content = conn.request(request_url)
    content = json.loads(content)
    return content['access_token']


if __name__ == '__main__':
    print menu
    app_id = 'wxcb9c2d19d6683ac2'
    app_secret = 'fb662c8b05fa80b9957b8387a2a7da5c'
    access_token = get_access_token(app_id, app_secret)
    print access_token

    create_menu_url = 'https://api.weixin.qq.com/cgi-bin/menu/create?access_token=%s' % access_token
    headers = {'content-type': 'text/html; charset=utf-8'}
    #headers = {'content-type': 'application/x-www-form-urlencoded', 'cache-control':'no-cache'}
    resp, content = conn.request(create_menu_url, 'POST', menu, headers)
    #resp, content = conn.request(create_menu_url, 'POST', urllib.urlencode(menu), headers)
    #print 'resp: %s, content: %s' % (resp, content)
    content = json.loads(content)
    if resp['status'] != '200':
        print resp
    if 'errcode' in content.keys():
        print 'error code: %s, error message: %s' % (content['errcode'], content['errmsg'])
